var express = require('express');
var add = require('./add');
var bodyParser = require('body-parser');
var upload = require('./fileupload');
var path = require('path');
var multer = require('multer');
var send  = require('./sendMail');
var files = require('./fs');

var app = express();
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded

app.get('/show.json', function (req, res) {
  res.send('Hello '+req.query.name+'!');
});
console.log(process.execPath);
console.log(__dirname);
console.log(process.cwd());
app.use('/api',add);
app.use('/api',files);
app.use('/src',express.static('frontend'));
app.use('/upload',express.static(path.join(__dirname, 'upload')));
//文件上传服务
app.post('/uploads', upload.single('image'), function (req, res, next) {
    if (req.file) {
        res.send(req.file);
        console.log(req.file);
        console.log(req.body);
    }
});
//发送邮件
app.get('/sendmail',function(req,res){
	console.log(req.query);
	var mes = req.query;
	send(mes.touser,mes.subject,'<h1>这是自动发送的邮件</h1>',function(err,response){
		res.send(response);
	})
})

var server = app.listen(3000, function () {
  var host = server.address().address;
  var port = server.address().port;

  console.log('Example app listening at http://%s:%s', host, port);
});